package com.jstyle.jclife.daoManager;




import android.text.TextUtils;

import com.jstyle.blesdk.model.ExerciseMode;
import com.jstyle.jclife.dao.PathRecordDao;
import com.jstyle.jclife.model.PathRecord;
import com.jstyle.jclife.utils.DbManager;
import com.jstyle.jclife.utils.SharedPreferenceUtils;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/10.
 */

public class PathRecordDaoManager {


    public static void insertPathRecord(PathRecord pathRecord){
        DbManager.getInstance().getDaoSession().getPathRecordDao().insert(pathRecord);
    }
    public static void insertPathRecord(List<PathRecord>pathRecordList){
        DbManager.getInstance().getDaoSession().getPathRecordDao().insertOrReplaceInTx(pathRecordList);
    }
    public static PathRecord getPathRecord(Long id){
        PathRecordDao pathRecordDao=DbManager.getInstance().getDaoSession().getPathRecordDao();
        QueryBuilder<PathRecord> builder = pathRecordDao.queryBuilder();
        builder.where(PathRecordDao.Properties.MId.eq(id));
        return builder.unique();
    }
    public static PathRecord getPathRecord(String date){
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);

        if(TextUtils.isEmpty(date)||TextUtils.isEmpty(address))return null;
        PathRecordDao pathRecordDao=DbManager.getInstance().getDaoSession().getPathRecordDao();
        QueryBuilder<PathRecord> builder = pathRecordDao.queryBuilder();
        builder.where(PathRecordDao.Properties.Address.eq(address),PathRecordDao.Properties.MDate.eq(date));
        return builder.unique();
    }
    public static List<PathRecord> getPathRecord(String startDate, String endDate){
        List<PathRecord> hrvDataList=new ArrayList<>();
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address))return hrvDataList;
        QueryBuilder<PathRecord> queryBuilder= DbManager.getInstance().getDaoSession().getPathRecordDao().queryBuilder();
        hrvDataList= queryBuilder.where(PathRecordDao.Properties.Address.eq(address),PathRecordDao.Properties.MDate.between(startDate+" 00:00:00",endDate+" 23:59:00")).orderDesc(PathRecordDao.Properties.MDate).list();
        return hrvDataList;
    }
    public static List<PathRecord> getAllPathRecord(){
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address))return new ArrayList<>();
        PathRecordDao pathRecordDao=DbManager.getInstance().getDaoSession().getPathRecordDao();
        QueryBuilder<PathRecord> builder = pathRecordDao.queryBuilder().where(PathRecordDao.Properties.Address.eq(address)).orderDesc(PathRecordDao.Properties.MDate);
        return builder.list();
    }
    public static void deleteAll(){
        DbManager.getInstance().getDaoSession().getPathRecordDao().deleteAll();;
    }

    public static List<PathRecord> getAllPathRecordByMode(){
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address))return new ArrayList<>();
        PathRecordDao pathRecordDao=DbManager.getInstance().getDaoSession().getPathRecordDao();
        QueryBuilder<PathRecord> builder1 = pathRecordDao.queryBuilder();
        QueryBuilder<PathRecord> builder = builder1.where(builder1.and(PathRecordDao.Properties.Address.eq(address),builder1.or(PathRecordDao.Properties.ExerciseMode.eq(ExerciseMode.Mode_CYCLING ),
                PathRecordDao.Properties.ExerciseMode.eq(ExerciseMode.Mode_RUN ))));
        return builder.list();
    }


    public static PathRecord getLastPathRecord() {
        QueryBuilder<PathRecord> queryBuilder = DbManager.getInstance().getDaoSession().getPathRecordDao().queryBuilder();
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address))return null;
        return queryBuilder.where(PathRecordDao.Properties.Address.eq(address)).list().size() == 0 ? null : queryBuilder.where(PathRecordDao.Properties.Address.eq(address)).orderDesc(PathRecordDao.Properties.MDate).list().get(0);
    }

    public static List<PathRecord> getAllPathRecordByModefortime(String startDate, String endDate){
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address))return new ArrayList<>();
        PathRecordDao pathRecordDao=DbManager.getInstance().getDaoSession().getPathRecordDao();
        QueryBuilder<PathRecord> builder1 = pathRecordDao.queryBuilder();
        QueryBuilder<PathRecord> builder = builder1.where(builder1.and(PathRecordDao.Properties.Address.eq(address),PathRecordDao.Properties.MDate.between(startDate+" 00:00:00",endDate+" 23:59:00"),builder1.or(PathRecordDao.Properties.ExerciseMode.eq(ExerciseMode.Mode_CYCLING ),
                PathRecordDao.Properties.ExerciseMode.eq(ExerciseMode.Mode_RUN ))));
        return builder.list();
    }
}
